package ru.ifmo.votinov;

import universum.bi.Constants;
import universum.bi.Location;
import universum.engine.topology.TorusTopology;
import universum.engine.Topology;

import java.util.ArrayList;

class SearchMap extends ArrayList<Location> {

    protected boolean initialized;
    private int networkStep;
    private Topology _topology = TorusTopology.getInstance(); // by cpp

    SearchMap(int networkStep) {
        initialized = false;
        this.networkStep = networkStep;
    }

    void init(Location location) {
        init(location, true);
    }

    void init(Location startLocation, boolean includeBorders) {
        int startX = startLocation.getX();
        int startY = startLocation.getY();
        int x = startX;
        while (x > 0 || (includeBorders && x == 0)) {
            init(x, startY, includeBorders);
            if (x > 0) {
                x = Math.max(0, x - networkStep);
            } else {
                x = -1;
            }
        }
        x = startX + networkStep;
        while (x < Constants.getWidth()) {
            init(x, startY, includeBorders);
            x += networkStep;
        }
        initialized = true;
    }

    private void init(int x, int startY, boolean includeBorders) {
        int y = startY;
        while (y > 0 || (includeBorders && y == 0)) {
            // [BEGIN] modified by cpp
            add(_topology.createLocation(x, y));
            //add(new Location(x, y));
            // [END] modified by cpp
            if (y > 0) {
                y = Math.max(0, y - networkStep);
            } else {
                y = -1;
            }
        }
        y = startY + networkStep;
        while (y < Constants.getHeight()) {
            // [BEGIN] modified by cpp
            //add(new Location(x, y));
            add(_topology.createLocation(x, y));
            // [END] modified by cpp
            y += networkStep;
        }
    }

    protected boolean isInitialized() {
        return initialized;
    }
}
